package com.swift.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.swift.common.constant.CommonConstant;
import com.swift.common.exception.BusinessException;
import com.swift.common.util.AESUtil;
import com.swift.common.util.DateUtil;
import com.swift.common.util.StringUtil;
import com.swift.core.user.dto.UserDto;
import com.swift.core.user.request.UserReq;
import com.swift.core.user.response.UserRes;
import com.swift.mapper.user.UserMapper;
import com.swift.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

/**
 * @author Administrator
 * @description :
 * @date 2022/06/18 16:17
 * @since open jdk 1.8
 */
@Service
public class UserServiceImpl implements UserService {
    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserRes queryUserInfoById(int id) {
        logger.info("获取用户信息,id:{}", id);
        UserDto userDto = userMapper.queryUserInfoById(id);
        UserRes userRes = new UserRes();
        BeanUtils.copyProperties(userDto,userRes,UserRes.class);
        return userRes;
    }

    @Override
   // @Transactional(rollbackFor = Exception.class)
    public void userAdd(UserReq user) throws UnknownHostException, BusinessException {
        logger.info("执行用户注册......");

        userDetailValid(user);
        UserDto userDto = new UserDto();
        InetAddress ip4Address = Inet4Address.getLocalHost();
        //用户姓名
        userDto.setUserName(user.getUserName());
        //性别
        userDto.setGender(user.getGender());
        String passWord = user.getPassWord();
        String salt = UUID.randomUUID().toString();
        //密码
        userDto.setPassWord(AESUtil.pwdEncode(passWord,salt));
        //盐
        userDto.setSalt(salt);
        //ip地址
        userDto.setIp4Address(JSON.toJSONString(ip4Address));
        //邮箱
        userDto.setEmail(user.getEmail());
        //用户地址
        userDto.setUserAddress(user.getUserAddress());
        //照片
        String userImg = user.getUserImg();
        if (StringUtil.isBlank(userImg)) {
            userDto.setUserImg("");
        } else {
            userDto.setUserImg(userImg);
        }
        //创建时间
        userDto.setCreateTime(user.getCreateTime());
        //修改时间
        userDto.setUpdateTime(DateUtil.getCurrentTime());
        //邮箱验证码
        userDto.setEmailVerified(user.getEmailVerified());
        //最近登录时间
        userDto.setLastLoginTime(DateUtil.getCurrentTime());
        //状态
        userDto.setState(CommonConstant.COMMON_NUMBER1);

        userMapper.userAdd(userDto);
    }

    private void userDetailValid(UserReq user) throws BusinessException {
        logger.info("用户注册信息校验......");

        if (StringUtil.isBlank(user.getUserName())) {
            throw new BusinessException("用户名不能为空");
        }
        if (StringUtil.isBlank(user.getPassWord())) {
            throw new BusinessException("用户密码不能为空");
        }
        if (StringUtil.isBlank(user.getEmail())) {
            throw new BusinessException("用户邮箱不能为空");
        }
        if (StringUtil.isBlank(user.getUserAddress())) {
            throw new BusinessException("用户地址不能为空");
        }
        if (StringUtil.isBlank(user.getEmailVerified())) {
            throw new BusinessException("用户邮箱验证码不能为空");
        }
        if (StringUtil.isBlank(user.getGender())) {
            throw new BusinessException("用户性别不能为空");
        }
        if (StringUtil.isBlank(user.getCreateTime())) {
            throw new BusinessException("用户创建时间不能为空");
        }
    }
}
